﻿using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Model;
namespace DAL
{
    /// <summary>
    /// it is suitable to the table with only one primary key,and can not deal with the aotu-increment key.
    /// Note:the first field in the table must be primary and autoincrement and int ,if exists 
    /// class:DAL ScoreInfo 
    /// Author:NetCenter
    /// DateTime:2021/5/23 15:00:51
    /// </summary>	
    public class DALScoreInfo
    {
        #region SQL CONST
        private const string INSERT_SQL = "INSERT INTO [ScoreInfo] ([ID],[Math],[Eng],[SubjectScore]) Values (@ID,@Math,@Eng,@SubjectScore)";
        private const string UPDATE_SQL = "UPDATE [ScoreInfo] SET Math=@Math,Eng=@Eng,SubjectScore=@SubjectScore WHERE [ID]=@ID";
        private const string DEL_SQL = "DELETE FROM [ScoreInfo] WHERE [ID]=@ID";
        private const string SELECT_ALL_SQL = "SELECT [ID],[Math],[Eng],[SubjectScore] FROM [ScoreInfo] order by iD desc";
        private const string SELECT_ONE_SQL = "SELECT [ID],[Math],[Eng],[SubjectScore] FROM [ScoreInfo] WHERE [ID]=@ID";
        private const string SELECT_WHERE_SQL = "SELECT [ID],[Math],[Eng],[SubjectScore] FROM [ScoreInfo] {$where} order by  iD desc";
        #endregion
        #region Paras const
        private const string PARA_ID = "@ID";
        private const string PARA_Math = "@Math";
        private const string PARA_Eng = "@Eng";
        private const string PARA_SubjectScore = "@SubjectScore";
        #endregion
        #region Add Method
        /// <summary>
        /// Insert an item
        /// </summary>
        /// <param name="ScoreInfo"></param>
        /// <returns>count of rows affected</returns>
        public int AddScoreInfo(ScoreInfoEntity ScoreInfo)
        {
            int backvalue = 0;
            //get paras
            SqlParameter[] ScoreInfoParms = GetScoreInfoParametersInsert();
            //set values to paras
            ScoreInfoParms[0].Value = ScoreInfo.ID;
            ScoreInfoParms[1].Value = ScoreInfo.Math;
            ScoreInfoParms[2].Value = ScoreInfo.Eng;
            ScoreInfoParms[3].Value = ScoreInfo.SubjectScore;
            //insert			
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, INSERT_SQL, ScoreInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Mod Method
        /// <summary>
        /// Update an item
        /// </summary>
        /// <param name="ScoreInfo"></param>
        /// <returns>count of rows affected</returns>
        public int ModScoreInfo(ScoreInfoEntity ScoreInfo)
        {
            int backvalue = 0;
            //get paras
            SqlParameter[] ScoreInfoParms = GetScoreInfoParameters();
            //set values to paras
            ScoreInfoParms[0].Value = ScoreInfo.ID;
            ScoreInfoParms[1].Value = ScoreInfo.Math;
            ScoreInfoParms[2].Value = ScoreInfo.Eng;
            ScoreInfoParms[3].Value = ScoreInfo.SubjectScore;
            //update			
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, UPDATE_SQL, ScoreInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Del Method
        /// <summary>
        /// Del an item by primary key
        /// </summary>
        /// <param name="iD"></param> 
        /// <returns>count of rows affected</returns>
        public int DelScoreInfo(string iD)
        {
            int backvalue = 0;
            //primarykeys
            //get paras
            SqlParameter[] ScoreInfoParms = GetScoreInfoPrimaryKeyParameters();
            //set values to paras
            ScoreInfoParms[0].Value = iD;
            //delete
            using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString))
            {
                backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, DEL_SQL, ScoreInfoParms);
            }
            return backvalue;
        }
        #endregion
        #region Select Method
        /// <summary>
        /// Select an item by Primarykey
        /// </summary>
        /// <param name="iD"></param>
        /// <returns>an item</returns>
        public ScoreInfoEntity GetScoreInfo(string iD)
        {
            ScoreInfoEntity ScoreInfo = new ScoreInfoEntity();
            //primarykeys
            //get paras
            SqlParameter[] ScoreInfoParms = GetScoreInfoPrimaryKeyParameters();
            //set values to paras
            ScoreInfoParms[0].Value = iD;
            //select
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ONE_SQL, ScoreInfoParms))
            {
                if (reader.Read())
                {
                    if (!reader.IsDBNull(0)) ScoreInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) ScoreInfo.Math = reader.GetDouble(1);
                    if (!reader.IsDBNull(2)) ScoreInfo.Eng = reader.GetDouble(2);
                    if (!reader.IsDBNull(3)) ScoreInfo.SubjectScore = reader.GetDouble(3);
                }
                else
                {
                    ScoreInfo = null;
                }
            }
            return ScoreInfo;

        }
        #endregion
        #region Select_All Method
        /// <summary>
        /// Select all items
        /// </summary>
        /// <returns>items</returns>
        public IList<ScoreInfoEntity> GetScoreInfos()
        {
            IList<ScoreInfoEntity> ScoreInfos = new List<ScoreInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ALL_SQL, null))
            {
                while (reader.Read())
                {
                    ScoreInfoEntity ScoreInfo = new ScoreInfoEntity();
                    if (!reader.IsDBNull(0)) ScoreInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) ScoreInfo.Math = reader.GetDouble(1);
                    if (!reader.IsDBNull(2)) ScoreInfo.Eng = reader.GetDouble(2);
                    if (!reader.IsDBNull(3)) ScoreInfo.SubjectScore = reader.GetDouble(3);
                    ScoreInfos.Add(ScoreInfo);
                }
            }
            return ScoreInfos;
        }
        #endregion
        #region private common select by conditions
        /// <summary>
        /// private common select by conditions
        /// </summary>
        /// <returns>IList<ScoreInfoEntity></returns>
        private IList<ScoreInfoEntity> GetScoreInfosbyQueryString(string SQLstr)
        {
            IList<ScoreInfoEntity> ScoreInfos = new List<ScoreInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SQLstr))
            {
                while (reader.Read())
                {
                    ScoreInfoEntity ScoreInfo = new ScoreInfoEntity();
                    if (!reader.IsDBNull(0)) ScoreInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) ScoreInfo.Math = reader.GetDouble(1);
                    if (!reader.IsDBNull(2)) ScoreInfo.Eng = reader.GetDouble(2);
                    if (!reader.IsDBNull(3)) ScoreInfo.SubjectScore = reader.GetDouble(3);
                    ScoreInfos.Add(ScoreInfo);
                }
            }
            return ScoreInfos;
        }
        #endregion
        #region public common select by conditions ,we can use it by changing the GetScoreInfosbyCondition(datatype condition) and sql
        /// <summary>
        /// Select items by Columns
        /// </summary>
        /// <returns>IList<ScoreInfoEntity></returns>
        public IList<ScoreInfoEntity> GetScoreInfosbyCondition(string sql)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(SELECT_WHERE_SQL);
            sb.Replace("{$where}", "where " + sql);
            return GetScoreInfosbyQueryString(sb.ToString());
        }
        #endregion
        #region Select Method Top n
        /// <summary>
        /// Select items by Columns
        /// </summary>
        /// <returns>IList<ScoreInfoEntity></returns>
        public IList<ScoreInfoEntity> GetScoreInfosTopN(int n)
        {
            IList<ScoreInfoEntity> ScoreInfos = new List<ScoreInfoEntity>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ALL_SQL, null))
            {
                int i = 0;
                while (reader.Read() && i < n)
                {
                    ScoreInfoEntity ScoreInfo = new ScoreInfoEntity();
                    if (!reader.IsDBNull(0)) ScoreInfo.ID = reader.GetString(0);
                    if (!reader.IsDBNull(1)) ScoreInfo.Math = reader.GetDouble(1);
                    if (!reader.IsDBNull(2)) ScoreInfo.Eng = reader.GetDouble(2);
                    if (!reader.IsDBNull(3)) ScoreInfo.SubjectScore = reader.GetDouble(3);
                    ScoreInfos.Add(ScoreInfo);
                    i++;
                }
            }
            return ScoreInfos;
        }
        #endregion
        #region paras configuration
        /// <summary>
        /// Internal function to get cached parameters:all paras --- suitable to insert ,especially autoincrement
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetScoreInfoParametersInsert()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_ALL_ScoreInfo_Insert");

            if (parms == null)
            {
                parms = new SqlParameter[] {
			new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
			new SqlParameter(PARA_Math,SqlDbType.Float) ,
			new SqlParameter(PARA_Eng,SqlDbType.Float) ,
			new SqlParameter(PARA_SubjectScore,SqlDbType.Float)			};
                SQLHelper.CacheParameters("SQL_ALL_ScoreInfo_Insert", parms);
            }
            return parms;
        }
        /// <summary>
        /// Internal function to get cached parameters:all paras --- suitable to update
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetScoreInfoParameters()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_ALL_ScoreInfo_Update");

            if (parms == null)
            {
                parms = new SqlParameter[] {
					new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
					new SqlParameter(PARA_Math,SqlDbType.Float) ,
					new SqlParameter(PARA_Eng,SqlDbType.Float) ,
					new SqlParameter(PARA_SubjectScore,SqlDbType.Float)					};
                SQLHelper.CacheParameters("SQL_ALL_ScoreInfo_Update", parms);
            }
            return parms;
        }
        /// <summary>
        /// Internal function to get cached parameters:primary key paras
        /// </summary>
        /// <returns></returns>
        private static SqlParameter[] GetScoreInfoPrimaryKeyParameters()
        {
            SqlParameter[] parms = SQLHelper.GetCachedParameters("SQL_PrimaryKey_ScoreInfo");

            if (parms == null)
            {
                parms = new SqlParameter[] {
					new SqlParameter(PARA_ID,SqlDbType.NVarChar) ,
					};
                SQLHelper.CacheParameters("SQL_PrimaryKey_ScoreInfo", parms);
            }
            return parms;
        }
        #endregion
    }
}
